<?php

$pageSize = $pagination['pageSize'];
$currentPage = $pagination['currentPage'];
$total = $pagination['total'];
$totalPage = ceil($total/$pageSize);

$pageKey = isset($pageKey) ? $pageKey : 'page';

$baseUrl = $this->request->getServer('REQUEST_URI');
$baseUrl = preg_replace("/(&)?".$pageKey."=[0-9]*/", '', $baseUrl);


if ($currentPage < 1) {
    $currentPage = 1;
}
if ($currentPage > $totalPage) {
    $currentPage = $totalPage;
}

$prevPage = ($currentPage > 1) ? ($currentPage - 1) : $currentPage;
$nextPage = ($currentPage < $totalPage) ? ($currentPage + 1) : $currentPage;

if ($totalPage > 1) {
    $beforeShowPoint = false;   // 前面的...是否已经显示
    $afterShowPoint = false;    // 后面的...是否已经显示
    ?>
    <style>
        .pagination-wrap {
            text-align: center;
            margin: 0 auto 20px;
            height: 50px;
        }
        .pagination {
            display: inline-block;
            vertical-align: middle;
        }
        .fast-lisnk {
            display: inline-block;
            vertical-align: middle;
            margin-left: 12px;
        }
    </style>
    <div class="pagination-wrap">
        <ul class="pagination">
            <?php if ((int)$currentPage == 1) { ?>
                <li class="active"><span>&lt;</span></li>
            <?php } else { ?>
                <li><a href="javascript:void(0)" onclick="jumpPage(<?php echo $prevPage;?>)">&lt;</a></li>
            <?php } ?>

            <?php for ($i = 1; $i <= $totalPage; $i ++) { ?>
                <?php
                $step = 3;
                if ($currentPage <= $step) {
                    $min = 1;
                    $max = 1 + $step*2;
                } else if ($currentPage > $step && $currentPage < ($totalPage - $step)) {
                    $min = $currentPage - $step;
                    $max = $currentPage + $step;
                } else if ($currentPage >= ($totalPage - $step)) {
                    $min = $currentPage - $step*2;
                    $max = $currentPage + 1;
                } else {
                }
                ?>
                <?php if ($i == $currentPage) { ?>
                    <li class="active"><span><?php echo $i; ?></span></li>
                <?php } else if ($i == 1 || $i == $totalPage || ($i >= $min && $i <= $max)) { ?>
                    <li><a href="javascript:void(0)" onclick="jumpPage(<?php echo $i; ?>)"><?php echo $i; ?></a></li>
                <?php } else {  ?>
                    <?php if ($beforeShowPoint == false && ($i - $step) > $min) { ?>
                        <li class=""><span>…</span></li>
                        <?php $beforeShowPoint = true; ?>
                    <?php } else if ($afterShowPoint == false && ($i + $step) < $max) { ?>
                        <li class=""><span>…</span></li>
                        <?php $afterShowPoint = true; ?>
                    <?php } ?>
                <?php } ?>
            <?php } ?>
            <?php if ((int)$currentPage == (int)$totalPage) { ?>
                <li class="active"><span>&gt;</span></li>
            <?php } else { ?>
                <li><a href="javascript:void(0)" onclick="jumpPage(<?php echo $nextPage;?>)">&gt;</a></li>
            <?php } ?>
        </ul>
        <div class="fast-lisnk">
            <span>共 <?php echo $totalPage; ?> 页，</span>
            <span>每页 <?php echo $pageSize; ?> 条，</span>
            <span>总计 <?php echo $total; ?> 条记录</span>
        </div>
    </div>
    
    <script type="text/javascript">
        function jumpPage(page) {
            var baseUrl = "<?php echo $baseUrl;?>";
            window.location = baseUrl + '&<?php echo $pageKey;?>=' + page;
        }
        function chkJump(e) {
            var p = jQuery('#jumpPageNumber').val();
            if (e.keyCode == 13) {
                if (jQuery.trim(p) == "") {
                    return false;
                }
                var pageNumber = parseInt(p);
                jumpPage(pageNumber);
            } else {
                var p = jQuery('#jumpPageNumber').val();
                p = p.replace(/[^\d]/g, '');
                jQuery('#jumpPageNumber').val(p);
            }
        }
    </script>
<?php
}
?>